#include "mini2440SettingForm.h"
#include "../ui_mini2440SettingForm.h"

mini2440SettingForm::mini2440SettingForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mini2440SettingForm)
{
    ui->setupUi(this);
    this->hide();
    //readData();
}

mini2440SettingForm::~mini2440SettingForm()
{
    delete ui;
}

void mini2440SettingForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void mini2440SettingForm::on_pbSave_clicked()
{
    //autoLock = QString::number(ui->spautolock->value());
    autoLock = ui->cbAutoLock->currentText().left(2).toInt();

    screenInter = ui->cbInterval->currentText().left(2).toInt();
    timeOut = ui->cbTimeout->currentText().left(2).toInt();
    //screenInter = QString::number(ui->spInterval->value());
    //timeOut = QString::number(ui->spDelay->value());
    language = ui->cbLanguage->currentText();
    qDebug() << timeOut;
    config->set_language(language);
    config->set_autolock(autoLock*1000);
    config->set_screenInterval(screenInter*1000);
    config->set_screenTimeout(timeOut*60000); // 60000
    config->saveConfig();
    language = config->get_language();
    emit saveSetting();
}


void mini2440SettingForm::readData()
{

//    ui->spautolock->setValue(autoLock);
//    ui->spInterval->setValue(screenInter/1000);
//    ui->spDelay->setValue(timeOut/60000); // 60000
  // AutoLock
    if (autoLock == 1000)
    {
        ui->cbAutoLock->setCurrentIndex(0);
    }
    else if (autoLock == 3000)
    {
        ui->cbAutoLock->setCurrentIndex(1);
    }
    else if (autoLock == 5000)
    {
        ui->cbAutoLock->setCurrentIndex(2);
    }
    else if (autoLock == 10000)
    {
        ui->cbAutoLock->setCurrentIndex(3);
    }
   // Interval
    if (screenInter == 1000)
    {
        ui->cbInterval->setCurrentIndex(0);
    }
    else if (screenInter == 5000)
    {
        ui->cbInterval->setCurrentIndex(1);
    }
    else if (screenInter == 10000)
    {
        ui->cbInterval->setCurrentIndex(2);
    }
    // TimeOut
    if (timeOut == 60000)
    {
        ui->cbTimeout->setCurrentIndex(0);
    }
    else if (timeOut == 180000)
    {
        ui->cbTimeout->setCurrentIndex(1);
    }
    else if (timeOut == 300000)
    {
        ui->cbTimeout->setCurrentIndex(2);
    }
    else if (timeOut == 600000)
    {
        ui->cbTimeout->setCurrentIndex(3);
    }


    if (language == "English")
    {
        ui->cbLanguage->setCurrentIndex(1);
    }
    else if (language == QString::fromUtf8("日本語"))
    {
        ui->cbLanguage->setCurrentIndex(0);
    }
    else if (language == QString::fromUtf8("Tiếng Việt"))
    {
        ui->cbLanguage->setCurrentIndex(2);
    }
}

void mini2440SettingForm::setConfig(mini2440Configuration *s_config)
{
    config = s_config;
    language = config->get_language();
    autoLock = config->get_autolock();
    screenInter = config->get_screenInterval();
    timeOut = config->get_screenTimeout();

    readData();
}

